package com.secneo.appshare.adapter;

import java.io.ByteArrayOutputStream;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.secneo.appshare.R;
import com.secneo.appshare.activities.ApplicationActivity;
import com.secneo.appshare.activities.AppsOtherUserActivity;
import com.secneo.appshare.activities.MemberSettingHonorActivity;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.remind.Remind;
import com.secneo.appshare.remind.Remind.RemindAction;

public class RemindAdapter extends BaseAdapter {

	Context mContext = null;
	List<Remind> mRemindList = null;

	public RemindAdapter(Context context, List<Remind> remindList) {
		mContext = context;
		mRemindList = remindList;
	}

	public void freeDrawable()
	{
		for(int i=0; i<mRemindList.size(); i++)
		{
			if (!mRemindList.get(i).getUserIconUrl().contains("buildin") && 
					mRemindList.get(i).userIcon != null)
			{
				BitmapDrawable a = (BitmapDrawable) mRemindList.get(i).userIcon;
				if(a != null && !a.getBitmap().isRecycled())
					a.getBitmap().recycle() ;
				mRemindList.get(i).userIcon = null ;
			}
			if(mRemindList.get(i).appIcon != null)
			{
				BitmapDrawable a = (BitmapDrawable) mRemindList.get(i).appIcon;
				if(a != null && a.getBitmap().isRecycled())
					a.getBitmap().recycle() ;
				mRemindList.get(i).appIcon = null;
			}
		}
		//mRemindList.clear();
		System.gc();
	}
	
	@Override
	public int getCount() {
		return mRemindList.size();
	}

	@Override
	public Object getItem(int position) {
		return mRemindList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RemindViewItem remindViewItem;
		final Remind remind = mRemindList.get(position);

		if (convertView == null) {
			View v = LayoutInflater.from(mContext).inflate(
					R.layout.appshare_activity_item, null);
			v.setPadding(8, 0, 8, 0);
			
			remindViewItem = new RemindViewItem();
			
			remindViewItem.mDataBackground = (LinearLayout) v
					.findViewById(R.id.data_background);
			remindViewItem.mUserIcon = (ImageView) v
					.findViewById(R.id.activity_user_icon);
			remindViewItem.mUserName = (TextView) v
					.findViewById(R.id.activity_user_name);
			remindViewItem.mData = (TextView) v
					.findViewById(R.id.activity_details);
			remindViewItem.mAppIcon = (ImageView) v
					.findViewById(R.id.activity_app_icon) ;
			remindViewItem.mDate = (TextView) v
					.findViewById(R.id.activity_date);
			
			v.setTag(remindViewItem);
			convertView = v;
		} else {
			remindViewItem = (RemindViewItem) convertView.getTag();
		}
		
		if (remind.getUserIcon() == null)
			remindViewItem.mUserIcon.setImageResource(R.drawable.some_user);
		else
			remindViewItem.mUserIcon.setImageDrawable(remind.getUserIcon());
		
		remindViewItem.mUserName.setText(remind.getUserName()) ;
		
		remindViewItem.mData.setText(remind.getData()) ;
		
		remindViewItem.mDate.setText(remind.getDate());
		
		remindViewItem.mDataBackground.setOnClickListener(
				new OnClickListener() {					
					@Override
					public void onClick(View v) {
						if(remind.getAction() == RemindAction.NEWSHAREACTION || 
								remind.getAction() == RemindAction.NEWCOLLECTIONACTION || 
								remind.getAction() == RemindAction.NEWCOLLECTIONACTION ||
								remind.getAction() == RemindAction.NEWDOWNLOADACTION){
							Intent intent = new Intent(); 
							 intent.setClass(mContext, ApplicationActivity.class);
		
							 Bundle mBundle = new Bundle(); 
							 mBundle.putString("package_name", remind.getPakageName());
							 mBundle.putInt("n", 20);
							 String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
							 mBundle.putString("uid", uid);
							 mBundle.putBoolean("installed_not_addicted", true);
							 
							 PackageManager manager = mContext.getPackageManager();
							 try 
							 {
								 PackageInfo info = manager.getPackageInfo(remind.getPakageName(), 0);
								 if (info != null)
								 {
									 mBundle.putBoolean("user_app_flag", true);
								 }
								 else
								 {
									 mBundle.putBoolean("user_app_flag", false);
								 }

							 } catch (Exception e) {
								 mBundle.putBoolean("user_app_flag", false);
							 }
							 
							 /* Send the icon */
							 mBundle.putString("app_icon", remind.getAppIconUrl());
							if (remind.getAppIcon() != null) 
							{
								try 
								{
									Bitmap bitmap = ((BitmapDrawable) remind.getAppIcon()).getBitmap();
									ByteArrayOutputStream stream = new ByteArrayOutputStream();
		
									bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
									byte[] bitmapdata = stream.toByteArray();
									mBundle.putByteArray("app_icon_array", bitmapdata);
									
								} catch (Exception ex) {
									ex.printStackTrace();
									mBundle.putByteArray("app_icon_array", null);
								}
		
							} else {
								mBundle.putByteArray("app_icon_array", null);
							}
							mBundle.putString("app_name", remind.getAppName());
							intent.putExtras(mBundle);
							mContext.startActivity(intent);
						} else if(remind.getAction() == RemindAction.NEWMEDALACTION){
							
							Intent intent = new Intent(mContext ,
									MemberSettingHonorActivity.class) ;
							mContext.startActivity(intent) ;
						}
					}
				}) ;
		
		remindViewItem.mUserIcon.setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent();
						intent.setClass(mContext, AppsOtherUserActivity.class);

						Bundle mBundle = new Bundle();
						String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
						mBundle.putString("uid", uid);
						mBundle.putString("other_uid", remind.getUserId());
						mBundle.putString("user_icon", remind.getUserIconUrl());
						mBundle.putString("user_name", remind.getUserName());
						intent.putExtras(mBundle);
						mContext.startActivity(intent);
					}
				}) ;
		
		if(remind.getAction() == RemindAction.NEWSHAREACTION ||
				remind.getAction() == RemindAction.NEWCOLLECTIONACTION ||
				remind.getAction() == RemindAction.NEWCOMMENTACTION ||
				remind.getAction() == RemindAction.NEWDOWNLOADACTION){
			if (remind.getAppIcon() == null)
				remindViewItem.mAppIcon.setImageResource(R.drawable.some_app);
			else
				remindViewItem.mAppIcon.setImageDrawable(remind.getAppIcon());
			
			remindViewItem.mAppIcon.setOnClickListener(
					new OnClickListener() {						
						@Override
						public void onClick(View v) {
							Intent intent = new Intent(); 
							 intent.setClass(mContext, ApplicationActivity.class);
		
							 Bundle mBundle = new Bundle(); 
							 mBundle.putString("package_name", remind.getPakageName());
							 mBundle.putInt("n", 20);
							 String uid = LocalStore.getLocaleStoreInstance(mContext).getUid();
							 mBundle.putString("uid", uid);
							 mBundle.putBoolean("installed_not_addicted", true);
							 
							 PackageManager manager = mContext.getPackageManager();
							 try 
							 {
								 PackageInfo info = manager.getPackageInfo(remind.getPakageName(), 0);
								 if (info != null)
								 {
			
									 
									 
									 mBundle.putBoolean("user_app_flag", true);
								 }
								 else
								 {
									 mBundle.putBoolean("user_app_flag", false);
								 }

							 } catch (Exception e) {
								 mBundle.putBoolean("user_app_flag", false);
							 }
							 
							 /* Send the icon */
							 mBundle.putString("app_icon", remind.getAppIconUrl());
							if (remind.getAppIcon() != null) 
							{
								try 
								{
									Bitmap bitmap = ((BitmapDrawable) remind.getAppIcon()).getBitmap();
									ByteArrayOutputStream stream = new ByteArrayOutputStream();
		
									bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
									byte[] bitmapdata = stream.toByteArray();
									mBundle.putByteArray("app_icon_array", bitmapdata);
									
								} catch (Exception ex) {
									ex.printStackTrace();
									mBundle.putByteArray("app_icon_array", null);
								}
		
							} else {
								mBundle.putByteArray("app_icon_array", null);
							}
							mBundle.putString("app_name", remind.getAppName());
							intent.putExtras(mBundle);
							mContext.startActivity(intent);
						}
					}) ;
		}
		
		return convertView;
	}

	class RemindViewItem {
		LinearLayout mDataBackground;
		ImageView mUserIcon;
		TextView mUserName;
		TextView mData;
		ImageView mAppIcon;
		TextView mDate;
	}

}
